/** @odoo-module **/

import {ControlPanel} from "@web/search/control_panel/control_panel";
import { SearchBar } from "@web/search/search_bar/search_bar";
import { SearchModel } from "@web/search/search_model";

import { _t } from "@web/core/l10n/translation";
import {registry} from "@web/core/registry";
import {useService} from "@web/core/utils/hooks";
import { download } from "@web/core/network/download";
import {Component, onWillStart, onMounted, useChildSubEnv, useState, useRef} from "@odoo/owl";

const { DateTime } = luxon;

export class SPlanAchievementRateReportComponent extends Component {
    setup() {
        this.orm = useService("orm");
        this.action = useService("action");

        this.state = useState({
            dropdownOpen: false,
            dateType: 'week',
            toDay: DateTime.now().toFormat("yyyy-MM-dd"),
            dateStr: '',
            header: [],
            lines: []
        })
        onWillStart(async () => {
            await this.getAchievementRate();
        });

    }

    async getAchievementRate() {
        const data = await this.orm.call(
            "s.plan.achievement.rate.report",
            "get_achievement_rate",
            [this.state.toDay, this.state.dateType]
        );

        this.state.header.splice(0, this.state.header.length, ...data.header);
        this.state.lines.splice(0, this.state.lines.length, ...data.lines);
        this.state.dateStr = data.dateStr
    }

    async chooseWeek() {
        this.state.dateType = 'week';
        this.state.dropdownOpen = false;
        await this.getAchievementRate()
    }

    async chooseMonth() {
        this.state.dateType = 'month';
        this.state.dropdownOpen = false;
        await this.getAchievementRate()
    }

    async onPreviousPeriodClicked(){
        if (this.state.dateType === 'month') {
            this.state.toDay = DateTime.fromISO(this.state.toDay).minus({ months: 1 }).toISODate();
        } else {
            this.state.toDay = DateTime.fromISO(this.state.toDay).minus({days: 7}).toISODate();
        }
        await this.getAchievementRate()
    }

    async onNextPeriodClicked(){
        if (this.state.dateType === 'month') {
            this.state.toDay = DateTime.fromISO(this.state.toDay).plus({ months: 1 }).toISODate();
        } else {
            this.state.toDay = DateTime.fromISO(this.state.toDay).plus({days: 7}).toISODate();
        }
        await this.getAchievementRate()
    }

    viewDetails(station_id, prod_date) {
        this.action.doAction({
            name: _t("Production Daily Line Report"),
            type: "ir.actions.act_window",
            res_model: "s.production.daily.line.report",
            views: [[false, "tree"]],
            target: "new",
            context: {
                station_id: station_id,
                prod_date: prod_date,
            },
        },);
    }

    async onTodayClicked(){
        this.state.toDay = DateTime.now().toFormat("yyyy-MM-dd")
        await this.getAchievementRate()
    }

    isWarning(v1, v2){
        return v1 < v2
    }

    async onDownload() {
        const url = `/download/sb/production/daily/report/excel/${this.state.dateType}/${this.state.toDay}`;
        const data = {};
        await download({ url , data });
    }
}

SPlanAchievementRateReportComponent.template = "sb_plan.SPlanAchievementRateReport";
SPlanAchievementRateReportComponent.components = {
    ControlPanel,
    // SearchBar
};
registry.category("actions").add("s_plan_achievement_rate_report", SPlanAchievementRateReportComponent);